home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d13 / ptv2n1.arc / STACKS.PAS < prev    next >
Pascal/Delphi Source File  |  1991-03-26  |  2KB  |  108 lines

  1. unit Stacks;
  2.  
  3. { Define two types of stack objects }
  4.  
  5. interface
  6.  
  7. uses BaseObj;
  8.  
  9. const MaxStackDepth = 10;
  10.  
  11. type StackPtr = ^Stack;
  12.      Stack = object(Base)
  13.        StackPointer: word;
  14.        Data: array[1..MaxStackDepth] of BasePtr;
  15.        constructor Init;
  16.        function Empty: boolean;
  17.        function Full: boolean;
  18.        procedure Push(var Obj: Base); virtual;
  19.        function Pop: BasePtr; virtual;
  20.        function DeepClone: BasePtr; virtual;
  21.        end;
  22.  
  23. type CloneStackPtr = ^CloneStack;
  24.      CloneStack = object(Stack)
  25.        constructor Init;
  26.        destructor Done; virtual;
  27.        procedure Push(var Obj: Base); virtual;
  28.        end;
  29.  
  30. implementation
  31.  
  32. constructor Stack.Init;
  33.   { Initialize a new Stack object. }
  34.   begin
  35.   StackPointer := 0
  36.   end;
  37.  
  38. function Stack.Empty: boolean;
  39.   { Check if the stack is currently empty. }
  40.   begin
  41.   Empty := StackPointer = 0
  42.   end;
  43.  
  44. function Stack.Full: boolean;
  45.   { Check if the stack is currently full. }
  46.   begin
  47.   Full := StackPointer = MaxStackDepth
  48.   end;
  49.  
  50. procedure Stack.Push(var Obj: Base);
  51.   { Push a pointer to an object onto the stack. }
  52.   begin
  53.   if not Full then
  54.     begin
  55.     inc(StackPointer);
  56.     Data[StackPointer] := @Obj
  57.     end
  58.   end;
  59.  
  60. function Stack.Pop: BasePtr;
  61.   { Pop a pointer to an object off of the stack. }
  62.   begin
  63.   if not Empty then
  64.     begin
  65.     Pop := Data[StackPointer];
  66.     dec(StackPointer)
  67.     end
  68.   end;
  69.  
  70. function Stack.DeepClone: BasePtr;
  71.   { Return a pointer to a deep clone of a stack. }
  72.   var I: word;
  73.       TempPtr: BasePtr;
  74.   begin
  75.   TempPtr := Base.DeepClone;
  76.   for I := 1 to StackPointer do
  77.     Data[I] := Data[I]^.DeepClone;
  78.   DeepClone := TempPtr
  79.   end;
  80.  
  81. constructor CloneStack.Init;
  82.   { Initialize a new CloneStack object. }
  83.   begin
  84.   Stack.Init
  85.   end;
  86.  
  87. destructor CloneStack.Done;
  88.   { Free the memory allocated to a CloneStack and its contents. }
  89.   var I: word;
  90.   begin
  91.   for I := 1 to StackPointer do
  92.     Data[I]^.Done;
  93.   Stack.Done
  94.   end;
  95.  
  96. procedure CloneStack.Push(var Obj: Base);
  97.   { Push a clone of an object onto the stack. }
  98.   var TempPtr: BasePtr;
  99.   begin
  100.   if not Full then
  101.     begin
  102.     TempPtr := Obj.Clone;
  103.     Stack.Push(TempPtr^)
  104.     end
  105.   end;
  106.  
  107. end.
  108.